package com.example.workrecord.service;

import com.alibaba.fastjson.JSONObject;
import com.example.workrecord.common.Page;
import com.example.workrecord.dao.BaseJPA;
import com.example.workrecord.dao.PlatFormDao;
import com.example.workrecord.entity.PlatForm;
import com.example.workrecord.entity.PlatFormb;
import com.example.workrecord.entity.QPlatForm;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PlatFormService extends BaseService<PlatForm,Long>{

    @Autowired
    private PlatFormbService formbService;
    @Autowired
    private PlatFormDao dao;

    @Autowired
    private JPAQueryFactory queryDslTool;


    @Override
    protected BaseJPA<PlatForm> getDao() {
        return dao;
    }
    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
    public void savePlatform(PlatForm form) {
        save(form);
        form.setHOrder(form.getId());
        update(form.getId(),form);
        List<PlatFormb> list=form.getPlatFormbs();
        if(null == list ||list.size()==0){
            return;
        }
        for(PlatFormb formb:list){
            formb.setPlatformId(form.getId());
        }
        formbService.saveAll(list);
    }
    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
    public void deletePlatform(Integer id) {
        delete(Long.valueOf(id));
        formbService.deleteByPlatForm(Long.valueOf(id));
    }

    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
    public void eidtPlatform(PlatForm form) {
        update(form.getId(),form);
        if(null != form.getPlatFormbs()&& form.getPlatFormbs().size()>0){
            formbService.editByform(form.getPlatFormbs(),form.getId());
        }
    }

    public void findUserByData(PlatForm vo, Page<PlatForm> page) {
        QPlatForm qPlatForm=QPlatForm.platForm;
        JPAQuery<PlatForm> jpaQuery=queryDslTool.select(qPlatForm).from(qPlatForm);
        if(null !=  vo.getCategory()){
            jpaQuery.where(qPlatForm.category.eq(vo.getCategory()));
        }
        if(null != vo.getName()){
            jpaQuery.where(qPlatForm.name.contains(vo.getName()));
        }
        if(null !=vo.getCategoryType()){
            jpaQuery.where(qPlatForm.categoryType.eq(vo.getCategoryType()));
        }
        if(null !=vo.getStatus()){
            jpaQuery.where(qPlatForm.status.eq(vo.getStatus()));
        }
        jpaQuery.orderBy(qPlatForm.hOrder.desc());
        long count=jpaQuery.fetchCount();
        List<PlatForm> forms=jpaQuery.offset((page.getPageCurrent() - 1) * page.getPageSize())
                .limit(page.getPageSize())
                .fetch();
        for(PlatForm form:forms){
            List<PlatFormb> formbs=formbService.findByPlatId(form.getId());
            form.setPlatFormbs(formbs);
        }
        page.setList(forms);
        page.setTotal(count);

    }

    public PlatForm findPlatFormById(PlatForm form) {
        form=findById(form.getId());
        List<PlatFormb> list=formbService.findByPlatId(form.getId());
        form.setPlatFormbs(list);
        return form;
    }
    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
    public void deleteAllByIds(JSONObject res) {
        String[] ids=res.getString("ids").split(",");
        for(String s:ids){
            PlatForm form=findById(Long.valueOf(s));
            if(null == form){
                continue;
            }
            List<PlatFormb> platFormbs= form.getPlatFormbs();
            if(null != platFormbs&& platFormbs.size()>0){
                formbService.deleteAll(platFormbs);
            }
            delete(Long.valueOf(s));
        }
    }
    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
    public void updateStatus(JSONObject res) {
        String[] ids=res.getString("ids").split(",");
        Integer status=res.getInteger("status");
        for(String s:ids) {
            PlatForm form=findById(Long.valueOf(s));
            if(null != form){
                form.setStatus(status);
                update(form.getId(),form);
            }
        }
    }
    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
    public void hOrder(List<PlatForm> list) {
        for(PlatForm form:list){
            PlatForm platForm=findById(form.getId());
            if(null != platForm){
                platForm.setHOrder(form.getHOrder());
                update(platForm.getId(),platForm);
            }
        }
    }
}
